package com.dayuanit.dy17.atm.controller;

import com.dayuanit.dy17.atm.config.AtmPropertiesConfig;
import com.dayuanit.dy17.atm.dto.ResponseDTO;
import com.dayuanit.dy17.atm.entity.User;
import com.dayuanit.dy17.atm.exception.BizException;
import com.dayuanit.dy17.atm.util.SignUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpSession;

public abstract class BaseController {
    protected static final String login_user = "loginUser";

    @Autowired
    protected AtmPropertiesConfig atmPropertiesConfig;

    @Autowired
    protected SignUtils signUtils;

    protected long getUserId(HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        return user.getId();
    }

    protected String getUsername(HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        return user.getUsername();
    }

    /**
     * 全局异常处理
     * @param be
     * @return
     */
    @ExceptionHandler(BizException.class)
    public ResponseDTO processBizException(BizException be) {
        be.printStackTrace();
        return ResponseDTO.fail(be.getMessage());
    }

    /**
     * 全局异常处理
     * @param e
     * @return
     */
    @ExceptionHandler({Exception.class})
    public ResponseDTO processException(Exception e) {
        e.printStackTrace();
        return ResponseDTO.fail("系统出错了，请联系客服!");
    }

}
